function tsSegmentRemoval(statName,OptsGen,MdateRemove,OptStr)
%tsSegmentRemoval - Removal of one or more segments of time series 
%
%   tsSegmentRemoval(statName,OptsGen,MdateRemove,OptStr)
%
% This function allows the removal of one or more segments from the time
% series related to the station statName, on the basis of the general
% StaVel options represented by OptsGen struct variable. 
%
% The input argument MdateRemove is a Nrem-by-2 matrix, where Nrem is 
% the number of time series segments to be removed, MdateRemove(k,1) and 
% MdateRemove(k,2) are the lower and higher date limit, expressed in  
% MATLAB serial form, for the k-th segment.
%
% The tsData file and the related mom files are automatically upgraded.
% If the input argument OptStr is 'Obs', the observation mom file is 
% upgraded. If OptStr is 'Raw', the raw mom file is upgraded. If OptStr
% is undefined, empty or is not 'Obs' or 'Raw', the default value 'Obs'
% is used.

% G. Teza, 2022.

if (nargin < 4) || isempty(OptStr) || ~ismember(OptStr,{'Obs','Raw'})
    OptStr = 'Obs';
end

if numel(MdateRemove) < 2
    return
end

if numel(MdateRemove) == 2
    t1 = MdateRemove(1);
    t2 = MdateRemove(2);
    nseg = 1;
else
    t1 = MdateRemove(:,1);
    t2 = MdateRemove(:,2);
    nseg = size(MdateRemove,1);
end

filenats = fullfile(OptsGen.dirTs,[statName OptsGen.AddTs '.mat']);
[ts,IT] = tsDataFileIn(filenats);
if ~IT
    return
end

MosMom = MosMomGen(ts,1); % Offsets matrix

% Initial properties:
dateS = ts.dateS; 
dateyfrac = ts.dateyfrac;
MJD = ts.MJD; 
GPSweek = ts.GPSweek;         
GPSday = ts.GPSday;         
t = ts.t;               
reflon = ts.reflon;          
E0 = ts.E0;              
Ed = ts.Ed;              
E = ts.E;               
N0 = ts.N0;                
Nd = ts.Nd;              
N = ts.N;               
V0 = ts.V0;              
Vd = ts.Vd;             
V = ts.V;              
antH = ts.antH;               
sE = ts.sE;              
sN = ts.sN;              
sV = ts.sV;              
cEN = ts.cEN;             
cEV = ts.cEV;            
cNV = ts.cNV;             
Lat = ts.Lat;            
Lon = ts.Lon;             
Height = ts.Height;          

Irem = [];
for k = 1:nseg
   t1k = t1(k);
   t2k = t2(k);
   [~,I1k] = min(abs(t-t1k));
   [~,I2k] = min(abs(t-t2k));
   Ik = I1k:I2k;
   Irem = [Irem Ik]; %#ok<AGROW>
end

Irem = Irem';

dateS(Irem,:) = [];  % note that this is a char array!!!  
dateyfrac(Irem) = [];
MJD(Irem) = []; 
GPSweek(Irem) = [];         
GPSday(Irem) = [];         
t(Irem) = [];               
reflon(Irem) = []; 
if ~isempty(E0)
    E0(Irem) = [];
    N0(Irem) = [];
    V0(Irem) = [];
end    
Ed(Irem) = [];              
E(Irem) = [];               
Nd(Irem) = [];              
N(Irem) = [];               
Vd(Irem) = [];             
V(Irem) = [];
if ~isempty(antH)
    antH(Irem) = [];
end
sE(Irem) = [];              
sN(Irem) = [];              
sV(Irem) = [];
if ~isempty(cEN)
    cEN(Irem) = [];            
    cEV(Irem) = [];            
    cNV(Irem) = [];
end
if ~isempty(Lat)
    Lat(Irem) = [];            
    Lon(Irem) = [];             
    Height(Irem) = [];
end

% upgrading properties:
ts.dateS = dateS; 
ts.dateyfrac = dateyfrac;
ts.MJD = MJD; 
ts.GPSweek = GPSweek;         
ts.GPSday = GPSday;         
ts.t = t;               
ts.reflon = reflon;          
ts.E0 = E0;              
ts.Ed = Ed;              
ts.E = E;               
ts.N0 = N0;                
ts.Nd = Nd;              
ts.N = N;               
ts.V0 = V0;              
ts.Vd = Vd;             
ts.V = V;              
ts.antH = antH;               
ts.sE = sE;              
ts.sN = sN;              
ts.sV = sV;              
ts.cEN = cEN;             
ts.cEV = cEV;            
ts.cNV = cNV;             
ts.Lat = Lat;            
ts.Lon = Lon;             
ts.Height = Height;

save(filenats,'ts');

if ~isempty(MosMom)
    MosNeu = MosMom2MosNeu(MosMom);
else
    MosNeu = [];
end

if strcmpi(OptStr,'Obs')
    dirOut = OptsGen.dirObs;
else
    dirOut = OptsGen.dirRaw;
end

writeNeuMom(...
    fullfile(dirOut,[statName OptsGen.AddObs]),...
    OptsGen.components,ts,MosNeu);